class MpdCallbacks
  
  #Only three callbacks are used:
  # state_callback: if mpd stops, why? and should something be cleaned up
  # current_song_callback: if the next song plays, the previous should be removed
  # playlist_length_callback: to make sure no one has added a song via another mpd client
  
  cattr_accessor :playlist_version
  @@playlist_version = Jukebox.radio.playlist_version
  
  #Eigen methodes
  
  def self.current_playlist_version
    Jukebox.radio.playlist_version
  end
  
  def self.current_song
    Jukebox.radio.current_song
  end
  
  def self.add_song_to_radio(song, position)
    log "Adding #{song.file} to the radio playlist"
    Jukebox.radio.add(song.file)
    Jukebox.if_not_playing_make_it_so
  end
  
  def self.remove_song_from_radio(song)
    log "Removing #{song.file} (#{song.pos}) from the radio playlist"
    Jukebox.radio.delete(song.pos)
  end
  
  def self.sync()
    log 'Syncing...'
    
    Jukebox.radio.playlist.each do |mpd_song|
      if not Jukebox.playlist.items.detect { |item| item.song.file == mpd_song.file rescue nil  }
        #The playlist does not contain the mpd song!
        log "There's a song in the mpd playlist, that shouldn't be there!"
        log "#{mpd_song.file} is not in Jukebox.playlist.items\n\n\n\n"
        self.remove_song_from_radio(mpd_song)
      end
    end
    
    Jukebox.playlist.items.each do |item|
      song = item.song
      
      next if song.nil?
      if not Jukebox.radio.playlist.detect { |mpd_song| song.file == mpd_song.file }
        
        #MPD does not contain the playlist song!
        log "Adding a song to the mpd playlist"
        self.add_song_to_radio(song, item.position)
      end
    end
    
    self.playlist_version = self.current_playlist_version
  end
 
  #Callback methodes
  
  def self.state_callback(state)
    log "New state: #{state}"
    
    Jukebox.radio.clear if state == 'stop' && Jukebox.radio.current_song.nil? && Jukebox.playlist.number_of_items <= 1
    #Jukebox.playlist.songs.destroy_all if state == 'stop' && Jukebox.radio.current_song.nil? && Jukebox.playlist.number_of_items <= 1
  end
  
  #Is this one used?
  def self.new_current_song_callback(song, previous_song = nil)
    log "current_song callback"
    log "#{song.to_yaml}"
    
    #Make sure the current song is marked as played
    Jukebox.playlist.current_item.mark_as_played if not Jukebox.playlist.current_item.nil?
    
    return false if song.nil?
    
    log "file #{song.file}"
    
    s = Jukebox.playlist.find_item_by_file(song.file)
    Jukebox.playlist.current_item = s
  end
  
  def self.playlist_length_callback(playlist_length = nil)
    return false if playlist_length == 0
    
    log "playlist_length: sync to make sure no one has added a song via another mpd client"
    
    self.sync
  end
  
  def self.next_song_callback(song)
    log 'Next callback'
    Jukebox.playlist.play_next_item
  end
  
  def self.previous_song_callback(song)
    log 'Previous callback'
    #Is in principe niet nodig?
  end
  
  #These are not used:
  
  def self.playlist_callback(version)    
  end
  
  def self.time_callback(elapsed_secs, total_secs)
    
  end
  
  def self.volume_callback(volume)
    
  end
    
  def self.repeat_callback(enabled)
    
  end
  
  def self.random_callback(enabled)
    
  end
  
  def self.crossfade_callback(enabled)
    
  end
  
  def self.current_songid_callback(song_id)
    
  end
  
  def self.bitrate_callback(bitrate)
    
  end
  
  def self.audio_callback(samplerate, bitrate)
    
  end
  
  def self.connection_callback(connected)
    
  end
  
  def self.log(info)
    ActiveRecord::Base.logger.info "#{info}\n"
    #logger.info "#{info}\n"
  end
  
end